package com.chen.controller;

import com.chen.model.Customer;
import com.chen.service.CustomerService;
//import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
//import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

//@RunWith(SpringJUnit4ClassRunner.class)
@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;


    @RequestMapping("/customer-add")
    public String customerAdd(){
        return "customer-add";
    }

    @RequestMapping("/toAddCustomer")
    public String toAddCustomer(Customer customer){
        customerService.toAddCustomer(customer);
        return "success";
    }

    @RequestMapping("/findAllCustomer")
    public String findAllCustomer(Model model){
        List<Customer> customerList;
        customerList = customerService.findAllCustomer();
        for(Customer customer:customerList){
            System.out.println(customer);
        }
        model.addAttribute("customerList",customerList);
        return "customer-list";
    }

//    @Test
//    public void test(){
//        List<Customer> customerList;
//        customerList = customerService.findAllCustomer();
//        for(Customer customer:customerList){
//            System.out.println(customer);
//        }
//    }

    @RequestMapping("/customer-edit")
    public String customerEdit(Integer customer_id,Model model){
        Customer customer = customerService.findCustomerById(customer_id);
        model.addAttribute("customer",customer);
        return "customer-edit";
    }

    @RequestMapping("/updateCustomer")
    public String updateCustomer(Customer customer){
//        Customer customer = customerService.findCustomerById(customer_id);
        System.out.println(customer);
        customerService.updateCustomer(customer);
        return "success";
    }
    @RequestMapping("/findCustomerById")
    public String  findCustomerById(Integer customer_id , Model model){
        Customer customer = customerService.findCustomerById(customer_id);
        List<Customer> customerList = new ArrayList<>();
        if (customer!=null)
            customerList.add(customer);
        model.addAttribute("customerList",customerList);
        return "customer-list";
    }
}
